package com.foolishbird.sdk.ui.fragment;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.foolishbird.sdk.core.HttpEngineApi;
import com.foolishbird.sdk.params.FloatGameServerResponseParams;
import com.foolishbird.sdk.params.ResponseParams;
import com.foolishbird.sdk.params.bean.FloatGameServer;
import com.foolishbird.sdk.params.bean.RedPackageInfo;
import com.foolishbird.sdk.params.bean.State;
import com.foolishbird.sdk.ui.assist.OnFinishActivityListener;
import com.foolishbird.sdk.utils.MyFragmentManger;
import com.foolishbird.sdk.utils.ResUtils;
import com.foolishbird.sdk.utils.WL;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Transformation;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


public class RedpackageOpenFragment extends BaseHttpFragment implements View.OnClickListener {

    private ImageView backView,openImage,avatarImage;
    private TextView nameText;

    public static final RedpackageOpenFragment newInstance(Bundle bundle) {
        RedpackageOpenFragment fragment = new RedpackageOpenFragment();
        fragment.setArguments(bundle);
        return fragment;
    }
    private RedPackageInfo info;
    private int taskId;
    private boolean isBinded;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        int resource = ResUtils.getLayoutIdentifier(getActivity(), "fb_redpackage_open");
        View contentView =inflater.inflate(resource,null);
        backView = contentView.findViewById(getViewIdentifier("redpackage_open_close"));
        backView.setOnClickListener(this);
        nameText = contentView.findViewById(getViewIdentifier("redpackage_open_name"));
        openImage = contentView.findViewById(getViewIdentifier("redpackage_open_btn"));
        avatarImage = contentView.findViewById(getViewIdentifier("redpackage_open_avatar"));
        openImage.setOnClickListener(this);
        info = (RedPackageInfo)getArguments().getSerializable("info");
        isBinded = getArguments().getBoolean("isBinded");
        if(!TextUtils.isEmpty(info.getAvatar())){
            Picasso.with(getContext()).load(info.getAvatar()).transform(new CircleTransform()).into(avatarImage);

        }
        nameText.setText(info.getNickName());
//        HttpEngineApi.getInstance().getFloatGameServer(requestCallbackHandler(FloatGameServerResponseParams.class));
        return contentView;

    }



    @Override
    public void onAsyncRequestResult(int flag, ResponseParams response, Object... params) {
        super.onAsyncRequestResult(flag, response, params);
        if (response != null) {
            int code = response.getState().getCode();
            if (State.SUCCESS == code) {
                if (taskId == flag) {
                    MyFragmentManger.getInstance().goBack(this);
                    MyFragmentManger.getInstance().gotoFragment(RedpackageResultFragment.newInstance(getArguments()));
                    AnimationDrawable animationDrawable = (AnimationDrawable)openImage.getBackground();
                    animationDrawable.stop();
                }
//            }

            } else {
                if (taskId == flag) {
                    Toast.makeText(getContext(), response.getState().getMsg(), Toast.LENGTH_SHORT).show();
                    AnimationDrawable animationDrawable = (AnimationDrawable)openImage.getBackground();
                    animationDrawable.stop();
                }
            }

        }

        WL.d("FloatNoticeFragment",response.toString());
    }



    @Override
    public void onClick(View view) {
        if(view ==backView){
            MyFragmentManger.getInstance().goBack(this);
        }else if(view==openImage){
            if(!isBinded){
                Toast.makeText(getActivity(),"请先绑定微信",Toast.LENGTH_SHORT).show();
                return;
            }
            openImage.setClickable(false);
            AnimationDrawable animationDrawable = (AnimationDrawable)openImage.getBackground();
            animationDrawable.start();//启动动画
            taskId =  HttpEngineApi.getInstance().redpackageOpen(info.getId(),requestCallbackHandler(FloatGameServerResponseParams.class));
//            MyFragmentManger.getInstance().goBack(this);
//            MyFragmentManger.getInstance().gotoFragment(RedpackageResultFragment.newInstance(null));
        }
//        OnFinishActivityListener listener = getOnFinishActivityListener();
//        if (listener != null) {
//            listener.doFinishActivity(this);
//        }
    }

    class CircleTransform implements Transformation {
        @Override
        public Bitmap transform(Bitmap source) {
            /**
             * 求出宽和高的哪个小
             */
            int  size = Math.min(source.getWidth(), source.getHeight());

            /**
             * 求中心点
             */
            int x = (source.getWidth() - size) / 2;
            int y = (source.getHeight() - size) / 2;

            /**
             * 生成BitMap
             */
            Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
            if (squaredBitmap != source) {
                //释放
                source.recycle();
            }

            /**
             * 建立新的Bitmap
             */
            Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());

            /**
             * 画布画笔
             */
            Canvas canvas = new Canvas(bitmap);
            Paint paint  = new Paint();

            BitmapShader shader = new BitmapShader(squaredBitmap,
                    BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
            paint.setShader(shader);
            paint.setAntiAlias(true);

            float r = size / 2f;
            /**
             * 画圆
             */
            canvas.drawCircle(r, r, r, paint);

            squaredBitmap.recycle();
            return bitmap;
        }

        @Override
        public String key() {
            return "circle";
        }
    }

}
